<?php

require_once( 'apikey.php' );
require_once( 'HTTPRequest.class.php' );

class Client {
	private static $baseURL = 'http://finappsapi.bdigital.org/api/2012/';
	private $token = '';
	
	public function __construct( $token ) {
		$this->token = $token;
	}
	
	public static function login( $user, $pass ) {
		if( empty( $user ) || empty( $pass) )
		  return false;
		
		$uri = $GLOBALS['apikey'].'/access/login';
		
		$request = new HTTPRequest( self::$baseURL.$uri, null, 'GET', 'application/json' );
		
		$request->addHeader( 'Authorization', 'Basic '.base64_encode( $user.':'.$pass ) );
		
		$response = $request->process();
		
		if( $response )
		  return json_decode( $response, true );
		else
		  return false;
	}
	
	public static function register( $userdata ) {
		if( empty( $userdata['username'] ) || empty( $userdata['password'] ) || empty( $userdata['firstName'] ) || empty( $userdata['lastName'] ) || empty( $userdata['address'] ) )
		  return false;
		  
		$uri = $GLOBALS['apikey'].'/access/client';
		  
		$jsondata = json_encode( $userdata );
		
		$request = new HTTPRequest( self::$baseURL.$uri, $jsondata, 'POST', 'application/json' );
		
		$response = $request->process();
    
    if( $response )
      return json_decode( $response, true );
    else
      return false;
	}
	
	public function setToken( $t ) {
		$this->token = $t;
	}
	
	public function getProfile() {
		$uri = $GLOBALS['apikey'].'/'.$this->token.'/operations/client/profile/@me';
    
    $request = new HTTPRequest( self::$baseURL.$uri, null, 'GET', 'application/json' );
    
    $response = $request->process();
    
    if( $response )
      return json_decode( $response, true );
    else
      return false;
	}
	
	public function createAccount( $userdata ) {
		if( empty( $userdata['office'] ) || !isset( $userdata['currency'] ) )
      return false;
      
    $uri = $GLOBALS['apikey'].'/'.$this->token.'/operations/account/@me';
      
    $jsondata = json_encode( $userdata );
    
    print_r( $jsondata );
    
    $request = new HTTPRequest( self::$baseURL.$uri, $jsondata, 'POST', 'application/json' );
    
    $response = $request->process();
    
    if( $response )
      return json_decode( $response, true );
    else
      return false;
	}
	
	public function getAccounts() {
		$uri = $GLOBALS['apikey'].'/'.$this->token.'/operations/client/profile/@me';
    
    $request = new HTTPRequest( self::$baseURL.$uri, null, 'GET', 'application/json' );
    
    $response = $request->process();
    
    if( $response )
      return json_decode( $response, true );
    else
      return false;
	}
	
  public function getAccountDetail( $ida ) {
    $uri = $GLOBALS['apikey'].'/'.$this->token.'/operations/account/'.$ida;
    
    $request = new HTTPRequest( self::$baseURL.$uri, null, 'GET', 'application/json' );
    
    $response = $request->process();
    
    if( $response )
      return json_decode( $response, true );
    else
      return false;
  }
  
  public function deposit( $userdata ) {
    if( empty( $userdata['accountNumber'] ) || !isset( $userdata['value'] ) || empty( $userdata['additionalData'] )  )
      return false;
      
    $uri = $GLOBALS['apikey'].'/'.$this->token.'/operations/account/deposit';
      
    $jsondata = json_encode( $userdata );
    
    print_r( $jsondata );
    
    $request = new HTTPRequest( self::$baseURL.$uri, $jsondata, 'POST', 'application/json' );
    
    $response = $request->process();
    
    if( $response )
      return json_decode( $response, true );
    else
      return false;
  }
  
  public function transfer( $userdata ) {  	
    if( empty( $userdata['originAccount'] ) || empty( $userdata['destinationAccount'] ) || !isset( $userdata['value'] ) || empty( $userdata['additionalData'] )  )
      return false;
      
    $uri = $GLOBALS['apikey'].'/'.$this->token.'/operations/account/transfer';
      
    $jsondata = json_encode( $userdata );
        
    $request = new HTTPRequest( self::$baseURL.$uri, $jsondata, 'POST', 'application/json' );
    
    $response = $request->process();
    
    if( $response )
      return json_decode( $response, true );
    else
      return false;
  }
}

?>